using MongoDB.Driver;
using Sustainalytics.DataLayer;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities.Governance;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.Governance.BusinessLogic.PdfMetadata
{
    public class GovernancePdfProfileMetadataRepository : RepositoryCollection<GovernancePdfProfileMetadata>, IGovernancePdfProfileMetadataRepository
    {
        private readonly MongoGridFSHelper mongoGridFSHelper;

        public GovernancePdfProfileMetadataRepository(string mongoDbUrl, string collectionName = null)
            : base(mongoDbUrl, collectionName)
        {
            if (string.IsNullOrEmpty(mongoDbUrl))
                throw new ArgumentException("mongoDbUrl");

            var url = new MongoUrl(mongoDbUrl);
            if (string.IsNullOrEmpty(url.DatabaseName))
                throw new ArgumentException("databaseName");

            var mongoClient = new MongoClient(mongoDbUrl);
            var database = mongoClient.GetDatabase(url.DatabaseName);
            mongoGridFSHelper = new MongoGridFSHelper(database);
        }

        public MongoGridFSHelper GetMongoGridFSHelper()
        {
            return mongoGridFSHelper;
        }

        public List<GovernancePdfProfileMetadata> ReadByCompanyId(Guid companyId)
        {
            return base.ReadAllWhere(x => x.CompanyId == companyId).ToList();
        }

        public GovernancePdfProfileMetadata GetFileMetadata(Guid fileId)
        {
            return base.ReadAllWhere(c => c.FileId == fileId).FirstOrDefault();
        }

        public GovernancePdfProfileMetadata GetFileMetadata(Guid companyId, GovernancePdfType pdfType)
        {
            return base.ReadAllWhere(c => c.CompanyId == companyId & c.GovernancePdfType == pdfType).FirstOrDefault();
        }

        public List<GovernancePdfProfileMetadata> GetAllProfileMetadata()
        {
            return base.ReadAll().ToList();
        }
    }
}
